// Sixun.SIString
using System;
using System.Text;

namespace CoreCmsUser.Net.Utility
{
	public static class KString
	{
		public static bool TryBool(object obj)
		{
			return TryBool(obj, decDef: false);
		}

		public static bool TryBool(object obj, bool decDef)
		{
			bool decRet = false;
			if (object.Equals(obj, DBNull.Value) || obj == null || object.Equals(obj, string.Empty))
			{
				return decDef;
			}
			if (bool.TryParse(obj.ToString(), out decRet))
			{
				return decRet;
			}
			return decDef;
		}

		public static int TryInt(object obj)
		{
			return TryInt(obj, 0);
		}

		public static int TryInt(object obj, int defInt)
		{
			int inttemp = 0;
			if (obj == null || obj == DBNull.Value || obj.Equals(string.Empty))
			{
				return defInt;
			}
			obj = obj.ToString().Replace("￥", "").Replace("$", "");
			if (obj.ToString().Contains("."))
			{
				obj = float.Parse(obj.ToString());
			}
			if (int.TryParse(obj.ToString(), out inttemp))
			{
				return inttemp;
			}
			return defInt;
		}

		public static long TryLong(object obj)
		{
			return TryLong(obj, 0);
		}

		public static long TryLong(object obj, int defLong)
		{
			long inttemp = 0L;
			if (obj == null || obj == DBNull.Value || obj.Equals(string.Empty))
			{
				return defLong;
			}
			obj = obj.ToString().Replace("￥", "").Replace("$", "");
			if (obj.ToString().Contains("."))
			{
				obj = float.Parse(obj.ToString());
			}
			if (long.TryParse(obj.ToString(), out inttemp))
			{
				return inttemp;
			}
			return defLong;
		}

		public static float TryFloat(object obj)
		{
			return TryFloat(obj, 0L);
		}

		public static float TryFloat(object obj, long defLong)
		{
			float inttemp = 0f;
			if (obj == null || obj == DBNull.Value || obj.Equals(string.Empty))
			{
				return defLong;
			}
			obj = obj.ToString().Replace("￥", "").Replace("$", "");
			if (obj.ToString().Contains("."))
			{
				obj = float.Parse(obj.ToString());
			}
			if (float.TryParse(obj.ToString(), out inttemp))
			{
				return inttemp;
			}
			return defLong;
		}

		public static short TryShort(object obj)
		{
			return TryShort(obj, 0);
		}

		public static short TryShort(object obj, short defInt)
		{
			short inttemp = 0;
			if (obj == null || obj == DBNull.Value || obj.Equals(string.Empty))
			{
				return defInt;
			}
			obj = obj.ToString().Replace("￥", "").Replace("$", "");
			if (obj.ToString().Contains("."))
			{
				obj = short.Parse(obj.ToString());
			}
			if (short.TryParse(obj.ToString(), out inttemp))
			{
				return inttemp;
			}
			return defInt;
		}

		public static decimal TryDec(object obj)
		{
			return TryDec(obj, 0m);
		}

		public static decimal TryDec(object obj, decimal defDec)
		{
			decimal dectemp = default(decimal);
			if (obj == null || obj == DBNull.Value || obj.Equals(string.Empty))
			{
				return defDec;
			}
			obj = obj.ToString().Replace("￥", "").Replace("$", "");
			if (decimal.TryParse(obj.ToString(), out dectemp))
			{
				return dectemp;
			}
			return defDec;
		}

		public static double TryDou(object obj, double defDou)
		{
			double doutemp = 0.0;
			if (obj == null || obj == DBNull.Value || obj.Equals(string.Empty))
			{
				return defDou;
			}
			obj = obj.ToString().Replace("￥", "").Replace("$", "");
			if (double.TryParse(obj.ToString(), out doutemp))
			{
				return doutemp;
			}
			return defDou;
		}

		public static char TryChar(object obj, char defstr)
		{
			if (object.Equals(obj, DBNull.Value) || obj == null)
			{
				return defstr;
			}
			char doutemp = '0';
			if (char.TryParse(TryStr(obj, defstr.ToString()), out doutemp))
			{
				return doutemp;
			}
			return defstr;
		}

		public static string TryStr(object obj)
		{
			return TryStr(obj, string.Empty);
		}

		public static string TryStr(object obj, string defstr)
		{
			if (object.Equals(obj, DBNull.Value) || obj == null)
			{
				return defstr;
			}
			return obj.ToString().Trim();
		}

		public static DateTime? TryDateTime(object obj)
		{
			if (object.Equals(obj, DBNull.Value) || obj == null || object.Equals(obj, string.Empty))
			{
				return null;
			}
			return Convert.ToDateTime(obj);
		}

		public static DateTime TryDateTime(object obj, DateTime defDt)
		{
			try
			{
				if (object.Equals(obj, DBNull.Value) || obj == null || object.Equals(obj, string.Empty))
				{
					return defDt;
				}
				return Convert.ToDateTime(obj);
			}
			catch
			{
				return defDt;
			}
		}

		public static string ToDBC(string input)
		{
			char[] c = input.ToCharArray();
			for (int i = 0; i < c.Length; i++)
			{
				if (c[i] == '\u3000')
				{
					c[i] = ' ';
				}
				else if (c[i] > '\uff00' && c[i] < '｟')
				{
					c[i] = (char)(c[i] - 65248);
				}
			}
			return new string(c);
		}

		public static char ToDBCChar(char input)
		{
			if (input == '\u3000')
			{
				input = ' ';
			}
			if (input > '\uff00' && input < '｟')
			{
				input = (char)(input - 65248);
			}
			return input;
		}

		public static string GetString(string str, int len)
		{
			if (string.IsNullOrEmpty(str) || len <= 0)
			{
				return string.Empty;
			}
			string[] strArray = toStringArray(str);
			return GetString(strArray, len);
		}

		public static string GetString(string[] strArray, int len)
		{
			int count = 0;
			StringBuilder sBuffer = new StringBuilder();
			int byteLen = 1;
			for (int i = 0; i < strArray.Length; i++)
			{
				byteLen = Encoding.Default.GetByteCount(strArray[i].ToString());
				count += byteLen;
				if (count <= len)
				{
					sBuffer.Append(strArray[i]);
					continue;
				}
				break;
			}
			if (sBuffer.Length > 0)
			{
				return sBuffer.ToString();
			}
			return string.Empty;
		}

		public static string GetString(string str, int count, bool isRight, bool isDel)
		{
			str = TryStr(str).Trim();
			string strRet = string.Empty;
			int byteLen = Encoding.Default.GetByteCount(str);
			if (byteLen > count)
			{
				strRet = ((!isDel) ? str : GetString(str, count));
			}
			else if (byteLen == count)
			{
				strRet = str;
			}
			else
			{
				strRet = str;
				if (isRight)
				{
					for (int j = 0; j < count - byteLen; j++)
					{
						strRet += " ";
					}
				}
				else
				{
					for (int i = 0; i < count - byteLen; i++)
					{
						strRet = " " + strRet;
					}
				}
			}
			return strRet;
		}

		public static string ReplaceEndChar(string str, string strReplace)
		{
			try
			{
				if (str == null)
				{
					return string.Empty;
				}
				if (string.IsNullOrEmpty(strReplace))
				{
					return str;
				}
				int interceptCount = Encoding.Default.GetByteCount(strReplace);
				if (interceptCount <= 0)
				{
					return str;
				}
				string[] strArray = toStringArray(str);
				if (strArray.Length == 0)
				{
					return string.Empty;
				}
				if (Encoding.Default.GetByteCount(str) > interceptCount)
				{
					return GetString(strArray, Encoding.Default.GetByteCount(str) - interceptCount) + strReplace;
				}
			}
			catch
			{
			}
			return str;
		}

		public static string[] toStringArray(string str)
		{
			string[] strArray = new string[str.Length];
			for (int i = 0; i < str.Length; i++)
			{
				strArray[i] = str.Substring(i, 1);
			}
			return strArray;
		}

		public static bool IsEqual(object strFirst, object strSend)
		{
			if (TryStr(strFirst).Trim().ToUpper() == TryStr(strSend).Trim().ToUpper())
			{
				return true;
			}
			return false;
		}

		public static int DivRem(int a, int b)
		{
			int intRet = 0;
			int intRem = 0;
			intRet = Math.DivRem(a, b, out intRem);
			if (intRem > 0)
			{
				intRet++;
			}
			return intRet;
		}
	}
}